package com.sdp.wx.controller;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_must_wz.entity.WxRepairMustWz;
import com.sdp.wx.db.wx_repair_must_wz.service.IWxRepairMustWzService;
import com.sdp.wx.db.wx_repair_must_wz.service.impl.WxRepairMustWzServiceImpl;
import com.sdp.wx.view.WxRepairMustWzView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-19
 */
@RestController
@RequestMapping("/WxRepairMustWzController")
public class WxRepairMustWzController {

	@Autowired
	private Query query;
	
	@Autowired
	private WxRepairMustWzServiceImpl WxRepairMustWzServiceImpl;
	
	@Autowired
	private IWxRepairMustWzService iWxRepairMustWzService;
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairMustWzView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(WxRepairMustWzServiceImpl, WxRepairMustWzView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/viewList")
	public Result<?> viewList(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairMustWzView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewList(WxRepairMustWzServiceImpl, WxRepairMustWzView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public synchronized Result<?> list(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("物资信息不存在");
		}
		String repairType = obj.getString("repairType");
		if(StringUtils.isEmpty(repairType)) {
			return Result.error("维修类型为空");
		}
		String vehicleModel = obj.getString("vehicleModel");
		if(StringUtils.isEmpty(repairType)) {
			return Result.error("车型为空");
		}
		
		List<WxRepairMustWz> wzs = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxRepairMustWz.class);
		for(WxRepairMustWz wz : wzs) {
			wz.setKeyId(null);
			wz.setRepairType(repairType);
			wz.setVehicleModel(vehicleModel);
		}
		
		if(!StringUtils.isEmpty(vehicleModel) && !StringUtils.isEmpty(repairType) && wzs!=null && wzs.size()>0) {
			List<WxRepairMustWz> wzList = iWxRepairMustWzService.queryRepairMustWzByRepairTypeAndVehicleModel(repairType,vehicleModel);
			if(wzList==null || wzList.size()==0) {
				iWxRepairMustWzService.saveBatch(wzs);
			}else {
				wzList.addAll(wzs);
				iWxRepairMustWzService.saveOrUpdateBatch(wzList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(WxRepairMustWz :: getWzNo))), ArrayList::new)));
			}
		}
		return Result.ok();
	}

	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxRepairMustWzService.removeById(keyId);
		return Result.ok();
	}
}

